#include "login.h"
#include "ui_login.h"
#include <QSqlDatabase>
#include <QMessageBox>
#include <QSqlTableModel>
#include "widget.h"
#include "booklist.h"

Login::Login(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Login)
{
    ui->setupUi(this);
    // 设置窗口标题
    this->setWindowTitle("图书馆书籍管理系统");
//    QPixmap pixmap(":/Login.png");
//    QPalette palette;
//    palette.setBrush(backgroundRole(),QBrush(pixmap));
//    setPalette(palette);
    setAutoFillBackground(true);

    ui->username->setText("yexing");
    ui->password->setText("2140707088");
}

Login::~Login()
{
    delete ui;
}

void Login::on_login_clicked()
{
    // 连接数据库
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("/home/hr/Desktop/088/sg.db");
    if(!db.open()){
        QMessageBox::information(this, "连接结果", "连接数据库失败!");
        return;
    }

    QString name = ui->username->text();
    QString passwd = ui->password->text();

    QSqlTableModel model;
    model.setTable("account");
    model.setFilter(tr("username = '%1' and password = '%2'").arg(name).arg(passwd));
    model.select();

    if(model.rowCount() == 1){
        // 表示密码验证正确，登陆成功
        QMessageBox::information(this, "Login", "Login succeeded!");

        this->hide();
        BookList *bookList;
        bookList = new BookList(NULL);
        bookList->show();

    }
    else{
        QMessageBox::information(this, "Warning", "Incorrect username or password, please re-enter！");
        ui->username->clear();
        ui->password->clear();
    }
}
